<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form  :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="x栋" prop="name">
      <el-input  v-model="dataForm.name" placeholder="x栋"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="subName">
      <el-input v-model="dataForm.subName" placeholder="备注"></el-input>
    </el-form-item>
     <el-form-item label="分类">
    <div>
    <el-cascader style="width:85%"
      filterabl
      clearable 
      placeholder="试试搜索:分类名"
      v-model="paths"
      :options="catelist"
      :props="setting"
    ></el-cascader>
  </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return { 
        catelist:[],
        setting:{
        value: "id",
        label: "name",
        },
        paths:[],
        visible: false,
        dataForm: {
          cateId: 0,
          name: '',
          subName: '',
          deleted: '',
        },
        dataRule: {
          name: [
            { required: true, message: 'x栋不能为空', trigger: 'blur' }
          ],
          subName: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
           //获取分类列表
          getListCate(){
            this.$http({
          url: this.$http.adornUrl('/help/cate/findAll'),
          method: 'get',
        }).then(({data}) => {
           this.catelist=data.data
        }) 
           },

      init (id) {
         this.dataForm.id = id || 0
         this.getListCate();
        this.dataForm.cateId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.cateId) {
            this.$http({
              url: this.$http.adornUrl(`/help/catelist/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.catelist.name
                this.dataForm.subName = data.catelist.subName
                this.dataForm.deleted = data.catelist.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/help/catelist/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'subName': this.dataForm.subName,
                'deleted': this.dataForm.deleted,
                'cateId':this.paths[0],
              })
            }).then(({data}) => {
              this.paths=[];
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
